# coding=utf-8

import sys
import json

reload(sys)
sys.setdefaultencoding('utf8')


def tokenize(line):
    fields = line.split("|")
    rtn = []
    for field in fields:
        word = field.strip()
        if word != "" and (not word.startswith("-")):
            rtn.append(word)
    return rtn


def get_map(heads, tokens):
    rtn = {}
    for i in range(0, len(tokens)):
        rtn[heads[i]] = tokens[i]
    return rtn


def read(name):
    org_file = open(name)
    heads = []
    i = 0
    rtn = []
    for line in org_file.readlines():
        if line.startswith('|'):
            if i == 1:
                heads = tokenize(line)
            elif i >= 4:
                tokens = tokenize(line)
                m = get_map(heads, tokens)
                if m:
                    rtn.append(m)
            else:
                pass
            i = i + 1
    return rtn


def print_json(name):
    dictionaries = read(name)
    for dictionary in dictionaries:
        if dictionary:
            out = json.dumps(dictionary, ensure_ascii=False)
            print out
